package cn.edu.lingnan.before.serviceimpl;

import cn.edu.lingnan.before.mapper.GoodsTableMapper;
import cn.edu.lingnan.before.mapper.GoodsTypeMapper;
import cn.edu.lingnan.before.pojo.GoodsTable;
import cn.edu.lingnan.before.pojo.GoodsType;
import cn.edu.lingnan.before.serivce.GoodsTableService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodsTableServiceImpl implements GoodsTableService {
    // 商品持久层对象
    @Resource
    private GoodsTableMapper goodsTableMapper;
    // 商品类型持久层对象
    @Resource
    private GoodsTypeMapper goodsTypeMapper;
    @Override
    public String showIndexData(Model model) {
        QueryWrapper<GoodsTable> wrapper = new QueryWrapper<>();
        wrapper.eq("isAdvertisement",1);
        // 获取是广告的商品列表
        List<GoodsTable> list = goodsTableMapper.selectList(wrapper);
        model.addAttribute("advertisementGoods",list);
        // 获取商品类型列表
        List<GoodsType> goodsTypes = goodsTypeMapper.selectList(null);
        model.addAttribute("goodsType",goodsTypes);
        // 查询推荐商品
        QueryWrapper<GoodsTable> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("isRecommend",1);
        List<GoodsTable> recommendGoods = goodsTableMapper.selectList(wrapper1);
        model.addAttribute("recommendGoods",recommendGoods);
        // 查询最新商品
        QueryWrapper<GoodsTable> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("isAdvertisement",0);
        List<GoodsTable> lastedGoods = goodsTableMapper.selectList(wrapper2);
        model.addAttribute("lastedGoods",lastedGoods);
        return "before/index";
    }

    @Override
    public String goodsDetailById(String goodId, Model model) {
        QueryWrapper<GoodsTable> wrapper = new QueryWrapper<>();
        wrapper.eq("isAdvertisement",1);
        // 获取是广告的商品列表
        List<GoodsTable> list = goodsTableMapper.selectList(wrapper);
        model.addAttribute("advertisementGoods",list);
        // 获取商品类型列表
        List<GoodsType> goodsTypes = goodsTypeMapper.selectList(null);
        model.addAttribute("goodsType",goodsTypes);
        GoodsTable goodsTable = goodsTableMapper.selectById(goodId);
        GoodsType goodsType = goodsTypeMapper.selectById(goodsTable.getGoodstypeId());
        goodsTable.setTypename(goodsType.getTypeName());
        model.addAttribute("goods",goodsTable);
        return "before/goodsDetail";
    }
}
